package com.axter.chrome.bookmark;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * 书签属性
 * Created by zhaobo on 2017/7/28.
 */
public class Bookmark {
    /** 目录类型 */
    public static final int TYPE_DIR = 1;
    /** 链接类型 */
    public static final int TYPE_URL = 2;
    /** 标题 */
    private String title;
    /** 类型 */
    private int type;
    /** 链接地址 */
    private String url;
    /** 目录下(目录or链接) */
    private List<Bookmark> children;
    /** 父目录 */
    private Bookmark parent;
    /** 属性 */
    private TreeMap<String, String> attrs;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<Bookmark> getChildren() {
        return children;
    }

    public void setChildren(List<Bookmark> children) {
        this.children = children;
    }

    public TreeMap<String, String> getAttrs() {
        return attrs;
    }

    public void setAttrs(TreeMap<String, String> attrs) {
        this.attrs = attrs;
    }

    public void addBookmark(Bookmark result) {
        if (children == null) {
            children = new ArrayList<Bookmark>();
        }
        children.add(result);
    }

    public Bookmark getParent() {
        return parent;
    }

    public void setParent(Bookmark parent) {
        this.parent = parent;
    }

    /**
     * 获取完整路径
     *
     * @return
     */
    public List<String> getFullPath() {
        if (parent == null) {
            List<String> sb = new ArrayList<>();
            sb.add(title);
            return sb;
        } else {
            List<String> sb = new ArrayList<>();
            sb.add(title);
            getParentPath(parent, sb);
            return sb;
        }
    }

    /**
     * 获取父路径
     *
     * @param parent
     * @param sb
     */
    private void getParentPath(Bookmark parent, List<String> sb) {
        if (parent != null) {
            sb.add(0, parent.getTitle());
            getParentPath(parent.getParent(), sb);
        }
    }

    @Override
    public boolean equals(Object obj) {
        // 1.书签类型
        if (obj instanceof Bookmark) {
            // 2.链接类型,链接地址
            if (type == TYPE_URL && ((Bookmark) obj).type == TYPE_URL) {
                return url.equals(((Bookmark) obj).url);
                // 2.目录类型,比较目录名(其实没啥用,真正比较的应该是绝对路径)
            } else if (type == TYPE_DIR && ((Bookmark) obj).type == TYPE_DIR) {
                return title.equals(((Bookmark) obj).title);
            }
        }
        return false;
    }
}
